package com.authManager.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authManager.commons.feignApi.SysRoleApi;
import com.authManager.commons.feignApi.SysUserRoleApi;
import com.authManager.entity.SysRole;
import com.authManager.entity.SysUserRole;
import com.authManager.web.model.LayuiTableData;
import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultData;

@Controller
@RequestMapping(value = "role")
public class RoleController extends BaseController {
	public RoleController() {
		System.out.println("初始化-RoleController");
	}

	@Autowired
	SysRoleApi sysRoleApi;
	@Autowired
	SysUserRoleApi sysUserRoleApi;

	@RequestMapping("/list")
	public String list() {
		return "role/list";
	}

	@RequestMapping("/listData")
	@ResponseBody
	public LayuiTableData<SysRole> listData(@RequestParam(defaultValue = "1", required = false) Integer pageNum,
			@RequestParam(defaultValue = "10", required = false) Integer pageSize,
			@RequestParam(required = false) Long sysRoleId, @RequestParam(required = false) String name,
			@RequestParam(required = false) Integer seq, @RequestParam(required = false) String description,
			@RequestParam(required = false) String roleCode, @RequestParam(required = false) String start_creater,
			@RequestParam(required = false) String end_creater) {
		ResultData<AdminPage<SysRole>> data = sysRoleApi.listSysRole(pageNum, pageSize, sysRoleId, name, seq,
				description, roleCode, start_creater, end_creater);
		return new LayuiTableData<SysRole>(data);
	}

	@RequestMapping("/giveList")
	public String giveList(@RequestParam(required = true) String userId) {
		request.setAttribute("userId", userId);
		ResultData<AdminPage<SysRole>> roleData = sysRoleApi.listSysRole(1, 999999, null, null, null, null, null, null,
				null);
		ResultData<AdminPage<SysUserRole>> userRoleData = sysUserRoleApi.listSysUserRole(1, 999999, null, userId, null,
				null, null);
		request.setAttribute("roleListData", roleData.getData().getRecords());
		request.setAttribute("userRoleListData", userRoleData.getData().getRecords());
		return "role/giveList";
	}

	@RequestMapping("/addOrDelData")
	@ResponseBody
	public ResultData<Object> addOrDelData(@RequestParam(required = true) Long sysRoleId,
			@RequestParam(required = true) String userId) {
		return sysUserRoleApi.addOrDelData(userId, sysRoleId);
	}

	@RequestMapping("/addOrEdit")
	public String addOrEdit(Integer dataId) {
		request.setAttribute("data", new SysRole());
		if (dataId != null) {
			ResultData<SysRole> sysRoleData = sysRoleApi.getSysRole(dataId.longValue());
			request.setAttribute("data", sysRoleData.getData());
		} else {

		}
		return "role/addOrEdit";
	}

	@RequestMapping("/addOrEditData")
	@ResponseBody
	public ResultData<Object> addOrEditData(@RequestParam(required = false) Long sysRoleId,
			@RequestParam(required = false) String name, @RequestParam(required = false) Integer seq,
			@RequestParam(required = false) String description, @RequestParam(required = false) String roleCode) {
		if (sysRoleId == null) {
			return sysRoleApi.saveSysRole(name, seq, description, roleCode);
		} else {
			return sysRoleApi.editSysRole(sysRoleId, name, seq, description, roleCode);
		}
	}

	@RequestMapping("/delData")
	@ResponseBody
	public ResultData<Object> delData(@RequestParam(required = false) Long dataId) {
		return sysRoleApi.delSysRole(dataId);
	}
}
